<?php
include("connect.php");
?>

<?php
$css = '<link rel="stylesheet" type="text/css" media="all" href="asset/css/class_bookdetail.css">';
$title = 'Book Detail';
$pagename = 'book_detail';

$userbookid = $_REQUEST['userbookid'];
//$userbookid = 13;
$type = $_REQUEST["type"];
$commentid  = $_REQUEST["commentid"];	
if($type=='delete')
{

	$sql = "delete from t_message where type_id=0 and message_id=".$commentid;
	mysql_query($sql);
	//echo $sql;
	echo '<script>';
	echo "alert('Delete success!');";
	echo '</script>';
}

?>

<?php
$sql = "select user_category from m_user where user_id=".$_SESSION['userid'];
$retval = mysql_query( $sql, $con );
while($row = mysql_fetch_array($retval))
	{
		$user_category = $row[0];
	}
if(isset($_SESSION["userid"]))
	{
		if($user_category ==9)
		{
			include("header_loggedinadmin.php");
		}else
		{
			include("header_loggedin.php");
		}
	}
else
{
	include("header.php");
}
?>
<div id="fb-root"></div>
<script>(function(d, s, id) {
  var js, fjs = d.getElementsByTagName(s)[0];
  if (d.getElementById(id)) return;
  js = d.createElement(s); js.id = id;
  js.src = "//connect.facebook.net/en_GB/all.js#xfbml=1";
  fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));</script>

<div id="blue_bg">
	<div id="main">
        <div class="wrapper">
        	<div id="content">
            	<div id="white_box">
                    <div id="book_box">
                    	<div id="photo_box">
                    		<?php
                    			$count = 1;
                    			$sql = "select a.bookphoto_link from t_book_photo a where a.user_book_id = ".$userbookid;
                    			$sql.= " order by a.bookphoto_series asc";                 
                    			$retval = mysql_query( $sql, $con );
                    			if(! $retval )
								{
  									die('Could not get data: ' . mysql_error());
								}
								while($row = mysql_fetch_array($retval))
								{
									if($count ==3)
										echo '<a href=""><img src="images/upload/'.$row[0].'" class="photo_thumb_nopadding" /></a>';
									else
										echo '<a href=""><img src="images/upload/'.$row[0].'" class="photo_thumb" /></a>';
									$count+=1;
								}
                    			while($count<=3)
                    			{
                    				if($count ==3)
										echo '<a href=""><img src="images/book_detail/book-thumb.jpg" class="photo_thumb_nopadding" /></a>';
									else
										echo '<a href=""><img src="images/book_detail/book-thumb.jpg" class="photo_thumb" /></a>';
									$count+=1;
                    			}
                    		?>
                    
                            <div class="clear"></div>
                        </div>
                        <?php
                        	$sql = "select a.book_title,a.book_author,b.price,b.condition1,b.condition2,b.condition3,b.condition4,b.user_id,a.isbn"; 
                        	$sql.=" from m_book a,t_user_book b where a.book_id=b.book_id and b.user_book_id=".$userbookid;
                        	$retval = mysql_query( $sql, $con );
							if(! $retval )
							{
  								die('Could not get data: ' . mysql_error());
							}
							while($row = mysql_fetch_array($retval))
							{
								$title = $row[0];
								$author = $row[1];
								$price = $row[2];
								$cond1 = $row[3];
								$cond2 = $row[4];
								$cond3 = $row[5];
								$cond4 = $row[6];
								$userid = $row[7];
								$isbn = $row[8];
							}
                        ?>
                    	<table>
                            <tr>
                                <td  colspan="2" style="padding-bottom:5px;"><span class="bold">BOOK DETAIL</span></td>
                                <td></td>
                            </tr>
                            <tr>
                                <td width="110px"><span class="bold">Title</span></td>
                                <td> : </td>
                                <td><?php echo $title;?></td>
                            </tr>
                            <tr>
                                <td width="110px"><span class="bold">Price</span></td>
                                <td> : </td>
                                <td><?php echo "$ ".$price;?></td>
                            </tr>
                            <tr>
                                <td><span class="bold">Author</span></td>
                                <td> : </td>
                                <td><?php echo $author;?></td>
                            </tr>
                            <tr>
                                <td><span class="bold">ISBN Number</span></td>
                                <td> : </td>
                                <td><?php echo $isbn;?></td>
                            </tr>
                            <!--<tr>
                                <td><span class="bold">Description</span></td>
                                <td> : </td>
                                <td>this book is still in good condition, it is cover by plastic cover no pages tore</td>
                            </tr>-->
                            <tr>
                            	<td><span class="bold">Category</span></td>
                                <td> : </td>
                            	<?php
  									$sql = "select c.category_name,b.category_detail_name from t_book_category a ";
  									$sql .= "inner join t_detail_category b on b.category_detail_id = a.category_detail_id ";
  									$sql .= "inner join t_category c on b.category_id = c.category_id ";
  									$sql .= "inner join m_book d on d.book_id = a.book_id ";
  									$sql .= "inner join t_user_book e on e.book_id = a.book_id ";
  									$sql .= "where e.user_book_id=". $userbookid;
  									$retval = mysql_query( $sql, $con );
									if(! $retval )
									{
  										die('Could not get data: ' . mysql_error());
									}
									while($row = mysql_fetch_array($retval))
									{
										$cat .=$row[0].' : '.$row[1]."<br>";
									}
									echo '<td>'.$cat.'</td>';
 								 ?>
							</tr>
                            <tr>
                                <td><span class="bold">Binding</span></td>
                                <td> : </td>
                                <td>
                                	<ul class="star">
                                		<?php
                                			$count = 5;
                                			while($cond1>=1)
                                			{
                                				$cond1-=1;
                                				$count-=1;
                                				echo '<li><a href=""><img src="images/book_detail/yellow-star.png" /></a></li>';
                                			}
                                			while($count>1)
                                			{
                                				echo '<li><a href=""><img src="images/book_detail/grey-star.png" /></a></li>';
                                				$count-=1;
                                			}	
                                		?>   
                                	</ul>
                                </td>
                            </tr>
                            <tr>
                                <td><span class="bold">Pages</span></td>
                                <td> : </td>
                                <td>
                                	<ul class="star">
                                       <?php
                                			$count = 5;
                                			while($cond2>=1)
                                			{
                                				$cond2-=1;
                                				$count-=1;
                                				echo '<li><a href=""><img src="images/book_detail/yellow-star.png" /></a></li>';
                                			}
                                			while($count>1)
                                			{
                                				echo '<li><a href=""><img src="images/book_detail/grey-star.png" /></a></li>';
                                				$count-=1;
                                			}	
                                		?>   
                                	</ul>
                                </td>
                            </tr>
                            <tr>
                                <td><span class="bold">Cover</span></td>
                                <td> : </td>
                                <td>
                                	<ul class="star">
                                        <?php
                                			$count = 5;
                                			while($cond3>=1)
                                			{
                                				$cond3-=1;
                                				$count-=1;
                                				echo '<li><a href=""><img src="images/book_detail/yellow-star.png" /></a></li>';
                                			}
                                			while($count>1)
                                			{
                                				echo '<li><a href=""><img src="images/book_detail/grey-star.png" /></a></li>';
                                				$count-=1;
                                			}	
                                		?>                                  
                                	</ul>
                                </td>
                            </tr>
                            <tr>
                                <td><span class="bold">Overall</span></td>
                                <td> : </td>
                                <td>
                                	<ul class="star">
                                        <?php
                                			$count = 5;
                                			while($cond4>=1)
                                			{
                                				$cond4-=1;
                                				$count-=1;
                                				echo '<li><a href=""><img src="images/book_detail/yellow-star.png" /></a></li>';
                                			}
                                			while($count>1)
                                			{
                                				echo '<li><a href=""><img src="images/book_detail/grey-star.png" /></a></li>';
                                				$count-=1;
                                			}	
                                		?>   
                                	</ul>
                                </td>
                            </tr>
                            <tr>
                                <td><br /></td><td><br /></td><td><br /></td>
                            <tr>
                            <tr>
                            	<?php
                            		$sql = "select fname,lname,phonenumber,jcu_email,fb_account,twitter_account";
                            		$sql .= " from m_user where user_id= ".$userid;
                            		$retval = mysql_query( $sql, $con );
									if(! $retval )
									{
  										die('Could not get data: ' . mysql_error());
									}
									while($row = mysql_fetch_array($retval))
									{
										$name = $row[0]." ".$row[1];
										$phone = $row[2];
										$jcuemail = $row[3];
										$fb = $row[4];
										$twit = $row[5];
									}
                            	?>
                                <td colspan="3" style="padding-bottom:5px;"><span class="bold">CONTACT DETAIL</span></td>
                            <tr>
                                <td><span class="bold">Name</span></td>
                                <td> : </td>
                                <td><?php echo $name;?></td>
                            </tr>
                            <tr>
                                <td><span class="bold">E-mail</span></td>
                                <td> : </td>
                                <td><?php echo $jcuemail;?></td>
                            </tr>
                            <tr>
                                <td><span class="bold">Phone</span></td>
                                <td> : &nbsp;</td>
                                <td><?php echo $phone;?></td>
                            </tr>
                             <tr>
                                <td><span class="bold">Facebook</span></td>
                                <td> : &nbsp;</td>
                                <td><div class="fb-send" data-href="http://www.jcubooksquare.co.cc/book_detail.php?userbookid=<?php echo $userbookid;?>"></div></td>
                            </tr>

                            <tr>
                            	<td><span class="bold">Twitter</span></td>
                            	<td> : &nbsp;</td>
                            	<td><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://www.jcubooksquare.co.cc" data-text="I want to .." data-via="<?php echo $twitter;?>">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script></td>
                            </tr>
                        </table>
                    </div>
                    <div id="chatting_box">
                    <?php
                    	$sql = "select a.user_id,b.fname,b.lname, a.message_contain,a.message_date,a.message_id from t_message a, m_user b ";
                    	$sql .= " where type_id=0 and a.user_id = b.user_id and a.is_deleted = 0 and a.user_book_id = ".$userbookid;
                    	$sql .=" order by message_id asc";
                    	$retval = mysql_query( $sql, $con );
						if(! $retval )
						{
  							die('Could not get data: ' . mysql_error());
						}
						while($row = mysql_fetch_array($retval))
						{
							$name = $row[1]." ".$row[2];
							$contain = $row[3];
							$date = $row[4];
							$message_id = $row[5];
						//echo $userid." ".$row[0]." ".$_SESSION["userid"];
							//echo $message_id;
							if($userid == $row[0])
							{
					?>
								<div class="user_box">
                        			<img src="images/book_detail/user<?php echo $row[0];?>.jpg" class="user_photo" />
                        			
                        			                        			
                           			 <div class="user_chat_box">
                            			<a href="my_profile.php?userprofileid=<?php echo $row[0];?>"><?php echo $name;?></a><br />
                                		<?php echo $contain;?><?php
                        				if($userid == $_SESSION["userid"])
                        				{
                        				
                        				?>
                        				<a href="book_detail.php?userbookid=<?php echo $userbookid;?>&type=delete&commentid=<?php echo $message_id; ?>"><img src="images/book_detail/close-btn.jpg"    /></a>
                        				<!--  style="position:absolute;right:5px;top:5px;" -->
                        				<?php
                        				}
                        			?>
<br />
                                		<span class="time"><?php echo $date;?></span>
                            		</div>
                            		<div class="clear"></div>
                       			 </div>
					<?php
							}else
							{
					?>
								<div class="guess_box">
                        			<img src="images/book_detail/user<?php echo $row[0];?>.jpg" class="guess_photo" />
                        			
                        			

                          		     <div class="guess_chat_box">
                          			  	<a href="my_profile.php?userprofileid=<?php echo $row[0];?>"><?php echo $name;?></a><br />
                                		<?php echo $contain;?><?php
                        			
                        				if($userid== $_SESSION["userid"])
                        				{
                        				
                        					?>
                        				<a href="book_detail.php?userbookid=<?php echo $userbookid;?>&type=delete&commentid=<?php echo $message_id; ?>"><img src="images/book_detail/close-btn.jpg"   /></a>
                        				<?php
                        				}
                        			?><br />
                                		<span class="time"><?php echo $date;?></span>
                            		</div>
                           			 <div class="clear"></div>
                        		</div>
					<?php			
							}				
						}
                    ?>
                    	<script language="JavaScript">
      							function submitonEnter(){
								var keycode;
								if (window.event) keycode = window.event.keyCode;
									else if (e) keycode = e.which;
								if(keycode == "13"){
									//document.commentform.submit();
									var userbookid = $("input#userbookid").val();
									var comment = $("textarea#comment").val();
									var dataString = 'userbookid='+ userbookid +  '&comment=' + comment;																		$.ajax({url:"comment_ajax.php?"+dataString, success:function(result){$("#chatting_box").html(result);}});
								}
							}
						</script>
						<?php
						if(isset($_SESSION["userid"]))
						{
						?>
                    	<form method="post" action="#" name="commentform"/>
                    		<input type="hidden" value="<?php echo $userbookid;?>" id="userbookid" name="userbookid" />
                        	<div class="comment_box">
                        		<textarea rows="1" id="comment" name="comment" onKeyDown="submitonEnter();">write comment</textarea>
                        	</div>
                        </form>
                        <?php
                        }?>
           			</div>
                    <div class="clear"></div>
                </div>
            </div>
        </div>
    </div>
</div>



<?php
include("footer.php");
?>

